package com.xyc.student.controller;


import com.xyc.student.mapper.PageMapper;
import com.xyc.student.mapper.StuMapper;
import com.xyc.student.mapper.UserMapper;
import com.xyc.student.pojo.Page;
import com.xyc.student.pojo.Stu;
import com.xyc.student.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class LoginController {
    @Autowired
    UserMapper userMapper;

    @Autowired
    StuMapper stuMapper;

    @Autowired
    PageMapper pageMapper;

    @Autowired
    JavaMailSender javaMailSender;

    @Autowired
    RedisTemplate redisTemplate;

    //我要登录
    @PostMapping(value = "/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password,
                        Model model, HttpSession session, Map<String, Object> map) {
        // Page page = pageMapper.find(1);
        //System.out.println("显示page查询结果:" + page);
        if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
            List<User> userList = userMapper.find();
            map.put("from", 0);
            map.put("to", 6);
            List<Stu> stuList = stuMapper.pageLimit(map); // 0 - 6
            for (User user : userList) {
                if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                    model.addAttribute("stu", stuList);
                    session.setAttribute("loginUserName", username);
                    session.setAttribute("loginUserRole", user.getRole());
                    model.addAttribute("loginUser", username);
                    return "stu/list";
                }
            }
        } else {
            map.put("msg", "用户名或密码错误");
            return "login";
        }
        return "login";
    }

    //查看账号
    @RequestMapping("/look")
    public String look(Model model) {
        model.addAttribute("user", userMapper.find());
        return "stu/look";
    }

    @RequestMapping("/send")
    public String send() {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        String email = "2791486247@qq.com";
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setFrom(email);
        msg.setSubject("student验证码");
        int result = (int) (Math.random() * 90000 + 100000);
        // 产生随机验证码
        msg.setText(String.valueOf(result));
        jedis.set("myCode", String.valueOf(result));
        msg.setTo(email);
        try {
            javaMailSender.send(msg);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("发送邮件异常");
        }
        return "redirect:/index";
    }
}
